const localUrl = "http://192.168.2.163";
window.globel = window.globel || {};
window.globel.lastMouseHoverNode = null;
window.globel.currentNeo4jData = null;

function getAll3DTiles() {
    var requestUrl = localUrl + ":9800/rest/3dtiles/getAll3DTiles";

    fetch(requestUrl, {
        method: 'get',
    })
        .then(response => response.json())
        .then(data => {
            printAsTable(data);
            var nodes=createNeo4jStruct();
            for(var i in data){
                JSONToNeo4jNode(nodes,data[i]);
            }
            initGraph(nodes);
        })
        .catch((error) => {
            console.error('Error:', error);
        });
}
function getSubRelation() {
    let relationName = document.getElementById('subRelationName').value;
    let filterOfLeft = document.getElementById('filterOfLeft').value;
    let filterOfRight = document.getElementById('filterOfRight').value;
    window.globel.currentNeo4jData = null;

    getSubRelationWithOptions(relationName, filterOfLeft, filterOfRight)
}

function getSubRelationWithOptions(relationName, filterOfLeft, filterOfRight) {
    var requestUrl = localUrl + ":9800/rest/3dtiles/getSubRelation?relationName=" + relationName +
        "&filterOfLeft=" + filterOfLeft +
        "&filterOfRight=" + filterOfRight;

    fetch(requestUrl, {
        method: 'get',
    })
        .then(response => response.json())
        .then(data => {
            console.log(data);
            print(data);
            if(data.length==0)return;
            clearNodes(data[0]);
            if (!window.globel.currentNeo4jData) {
                window.globel.currentNeo4jData = data[0];
                initGraph(window.globel.currentNeo4jData);
            }
            else {
                appendData(window.globel.currentNeo4jData, data[0])
                neo4jd3.updateWithNeo4jData(data[0]);
            }
        })
        .catch((error) => {
            console.error('Error:', error);
        });
}

function getItemsFromNeo4j(){
    let itemName = document.getElementById('neo4jName').value;
    var requestUrl = localUrl + ":9800/rest/3dtiles/getItemsFromNeo4j?itemName=" + itemName;

    fetch(requestUrl, {
        method: 'get',
    })
        .then(response => response.json())
        .then(data => {
            printAsTable(data);
            var nodes=createNeo4jStruct();
            for(var i in data){
                JSONToNeo4jNode(nodes,data[i]);
            }
            initGraph(nodes);
        })
        .catch((error) => {
            console.error('Error:', error);
        });
}

function createNeo4jEntities(){
    let itemName = document.getElementById('toCreateEnitityName').value;
    var requestUrl = localUrl + ":9800/rest/3dtiles/createNeo4jEntities?enitityName=" + itemName;

    fetch(requestUrl, {
        method: 'get',
    })
        .then(response => response.json())
        .then(data => {
            print(data);
        })
        .catch((error) => {
            console.error('Error:', error);
        });
}

function setRelation(){
    let itemName = document.getElementById('toCreateRelationName').value;
    var requestUrl = localUrl + ":9800/rest/3dtiles/setRelation?relationName=" + itemName;

    fetch(requestUrl, {
        method: 'get',
    })
        .then(response => response.json())
        .then(data => {
            print(data);
        })
        .catch((error) => {
            console.error('Error:', error);
        });
}

function print(data) {
    var dom = document.getElementById('result');
    dom.innerText = JSON.stringify(data);
}
function printAsTable(data){
    var dom = document.getElementById('result');
    var tableStr=`
    <table border=1><tr>
    `
    
    for(var i in data){
        for(var c in data[i]){
            tableStr+="<td>"+c+"</td>";
        }
        tableStr+="</tr>"
        break;
    }
    var tbodyStr="";
    for(var i in data){
        tbodyStr+="<tr>";
        for(var c in data[i]){
            tbodyStr+="<td>"+data[i][c]+"</td>";
        }
        tbodyStr+="</tr>";
    }
    tableStr+=tbodyStr+"</table>";
    dom.innerHTML=tableStr;
}


function clearNodes(oldData){
    var oldNodes = oldData.results[0].data[0].graph.nodes;
    var oldNodesDir = {};
    for (var i = oldNodes.length-1;i>-1; i--) {
        if(oldNodesDir[oldNodes[i].id]){
            oldNodes.splice(i,0);
            continue;
        }
        oldNodesDir[oldNodes[i].id] = oldNodes[i];
    }
}
